def write_node_list(fo, cores_per_node, node):
    fo.write( "#PBS -l nodes=")
    fo.write(f"{node[0]}:ppn={cores_per_node}")
    for i in node[1:-1]:
        fo.write(f"+{i}:ppn={cores_per_node}")
    fo.write("\n")

def write_pbs_head(fo,subinfo,dir,nodes,jobname):
    fo.write( "#!/bin/bash -e\n" )
    fo.write( "#PBS -N {0}\n".format(jobname) )
    if isinstance(nodes, int):
        fo.write( "#PBS -l nodes={0}:ppn={1}\n".format(nodes,subinfo["cores_per_node"]))
    elif isinstance(nodes, list):
        write_node_list(fo, subinfo["cores_per_node"], nodes)
    fo.write( "#PBS -l walltime={0}\n".format(subinfo["run_time"]))
    if subinfo["memory"] is not None:
        fo.write( "#PBS -l mem={0}\n".format(subinfo["memory"]) )
    fo.write( "#PBS -q {0}\n".format(subinfo["queue"] ) )
    # fo.write("#PBS -o {0}/J${{PBS_JOBID}}/sub.result\n".format(dir))
    # fo.write("#PBS -o {0}/J${{PBS_JOBID}}/sub.erro\n".format(dir))
    fo.write("#PBS -o {0}/sub.result\n".format(dir))
    fo.write("#PBS -e {0}/sub.erro\n".format(dir))
    fo.write("\n")

def write_pbs_runenvironments(fo,threads,dir,runenvironments):
    hpctoolkit_tag = 0
    fo.write( f"WORKDIR={dir}\n\n" )
    fo.write( f"cd $WORKDIR\n\n" )
    fo.write("cat $PBS_NODEFILE > $WORKDIR/nodelist.pbs\n\n")
    # fo.write( "mkdir -p {0}/J${{PBS_JOBID}}\n".format(dir) )
    fo.write( f"export OMP_NUM_THREADS={threads}\n\n" )
    for i in runenvironments:
        if i == "mpi":
            mpi_dict = runenvironments[i]
            if mpi_dict["load_mpi_command"] is not None:
                fo.write( "{0}\n\n".format(mpi_dict["load_mpi_command"]) )
            if mpi_dict["add_other_mpi_env"] is not None:
                fo.write( "{0}\n\n".format(mpi_dict["add_other_mpi_env"]) )
        elif i == "hwloc":
            hwloc_dict = runenvironments[i]
            for j in hwloc_dict:
                if hwloc_dict[j] is not None:
                    fo.write( "{0}\n\n".format(hwloc_dict[j]) )
        elif i == "hpctoolkit":
            hpctoolkit_dict = runenvironments[i]
            if hpctoolkit_dict["load_hpctoolkit_command"] is not None:
                hpctoolkit_tag = 1
                fo.write( "{0}\n\n".format(hpctoolkit_dict["load_hpctoolkit_command"]) )
        elif i == "mpip":
            if runenvironments[i]["load_mpip_command"] is not None:
                fo.write( "{0}\n\n".format(hpctoolkit_dict["load_mpip_command"]) )
        elif i == "load_others_command":
            if runenvironments[i] is not None:
                fo.write( "{0}\n\n".format(runenvironments[i]) )
    if hpctoolkit_tag:
        runenvironments["use_hpctoolkit"] = 1
    fo.write( f"\n" )

def write_pbs_run_info(fo):
    #write run info
    fo.write( "echo \"Node info\"" + "\n" )
    fo.write( "pestat" + "\n" )
    fo.write( "echo \"=============================================\"" + "\n" )
    fo.write( "echo \"Starting program run at \" `date` " + "\n" )
    fo.write( "echo \"=============================================\"" + "\n" )
    fo.write( "echo \"# SYSTEM INFO\"" + "\n" )
    fo.write( "echo 'The hostname is ' `hostname`" + "\n" )
    #fo.write( "grep 'Linux' /etc/issue" + "\n" )
    fo.write( "grep 'model name' /proc/cpuinfo |cut -d: -f2 |uniq -c" + "\n" )
    fo.write( "grep 'cpu M' /proc/cpuinfo" + "\n" )
    fo.write( "grep 'MemTotal' /proc/meminfo" + "\n" )
    fo.write( "echo \"=============================================\"" + "\n" )
    fo.write( "echo \"# PBS INFO\"" + "\n" )
    # fo.write( "echo \"The PBS_O_WORKDIR is\" $PBS_O_WORKDIR" + "\n" )
    # fo.write( "echo \"The PBS_JOBID is\" $PBS_JOBID" + "\n" )
    # fo.write( "echo \"The PBS_O_QUEUE is\" $PBS_O_QUEUE" + "\n" )
    # fo.write( "echo \"The PBS_O_HOST is\" $PBS_O_HOST" + "\n" )
    fo.write( "NP=`cat $PBS_NODEFILE | wc -l`" + "\n" )
    fo.write( "echo \"The num of PBS_NODEFILE is\" $NP" + "\n" )
    fo.write( "echo \"The PBS_NODEFILE IS:\" " + "\n" )
    fo.write( "cat $PBS_NODEFILE" + "\n" )

def write_end_info(fo):
    fo.write( "echo \"=============================================\"" + "\n" )
    fo.write( "echo \"End task at \" `date` " + "\n" )
    fo.write( "echo \"=============================================\"" + "\n" )
    fo.write( "echo \"Node info\"" + "\n" )
    fo.write( "pestat" + "\n" )

def get_except_list():
    except_list = ['numprocess', 'case', 'nodes', 'threads', 'bind_type']
    return except_list